package com.qdlc.p2p.biz.processor.task;


import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.processor.event.NoticeValueEvent;
import com.qdlc.p2p.biz.util.mail.Mail;
import com.qdlc.p2p.common.core.queue.TaskProcessorException;
import com.qdlc.p2p.common.core.queue.ValueEvent;
import com.qdlc.p2p.dal.dao.NoticeDao;
import com.qdlc.p2p.dal.dto.Notice;

/**
 * 发送邮件
 * @author linya 2016-01-17
 */
@Service
public class NoticeEmailTaskProcessor extends AbstractTaskProcessor {

	private static Logger logger = Logger.getLogger(NoticeEmailTaskProcessor.class);
	
	@Resource
	NoticeDao noticeDao;
	
	@Override
	public boolean supports(ValueEvent event) {
		return event instanceof NoticeValueEvent;
	}

	@Override
	protected void process0(ValueEvent event) throws TaskProcessorException {
        NoticeValueEvent noticeEvent = (NoticeValueEvent) event;
        Notice notice = noticeEvent.getNotice();
		Mail mail = Mail.getInstance();
        mail.setTo(notice.getReceiveAddr());
        mail.setSubject(notice.getTitle());
        mail.setBody(notice.getContent());
        try {
            mail.sendMail();
            notice.setStatus(1);
            notice.setResult("ok");
            logger.debug("发送邮件成功！！");
        } catch (Exception e) {
            notice.setStatus(0);
            notice.setResult("fail");
            logger.error("发送邮件失败", e);
        }
        noticeDao.save(notice);
	}

}
